import Vue from 'vue'
import Vuex from 'vuex'

import { getAuthorityCode } from '@/api/commonRequest'
Vue.use(Vuex)

export default new Vuex.Store({
    // 存储状态值
    state: {
        // 所有菜单编码
        allAuthority: {},
        areaList:[],
        menuId: '',
        route: [],
        // 设备搜索
        equipList: '',
        // 分页设置
        pagination: {
            pageSize: 15,
            pageSizes: [10, 15, 20, 50]
        },
        authority: {
            appendable: 0,
            editable: 0,
            deletable: 0,
            pubable: 0
        },
        storage: {
            enable: false,
            data: {}
        },
        showMenu: false,
        sendArr: [],
        // showFloor: false,
        floorArr: [],
        // 紧急通信
        sessionDialog: false,
        addressList: [{label: '王婷', mobile: 13797499185}, {label: '余晓晨', mobile: 18668166808}],
        sessionList: [{
            deviceName:'单兵001',
            deviceCode:'yzxPDL8CA1BBR6GBPO3HN8'
        },
        {
            deviceName:'单兵002',
            deviceCode:'YZGjGHGDHDSHAH'
        },
        {
            deviceName:'单兵003',
            deviceCode:'YZGjGHGDHDSHAH'
        },
        {
            deviceName:'单兵004',
            deviceCode:'YZGjGHGDHDSHAH'
        },
        {
            deviceName:'单兵002',
            deviceCode:'YZGjGHGDHDSHAH'
        },
        {
            deviceName:'单兵002',
            deviceCode:'YZGjGHGDHDSHAH'
        }],
        // 联动信息 true
        // 融合通信 false
        isInfo: true,
        infoMessage: '',
        isReadyMeeting: false,
        isMinCommunicationDialog: false,
        isCircleArea: false,
        isForbidCircleArea: false,
        // 安防人员车辆预警数据
        securityWarningData: [],
        map: {}, // 地图图层
        mapmarks: {
            keysitemarklayer: '', // 重点场所图层
            keyplacesmarklayer: '', // 重点机构图层
            heatMap: null, // 事件热力图图层 首页
            eventslayer: null, // 事件图层 公共管理
            villagemarklayer: null, // 小区图层
            wanggelayer: null, // 网格图层
            simpleSoldierlayer: null, // 移动设备图层
            ARPositionlayer: null, // AR图层
            videolayer: null, // 视频图层
            keypersonlayer: null, // 关注人员图层
            keypersonlinelayer: null, // 关注人员轨迹图层
            runwaylayer: null, // 河道图层
            vehiclelayer: null, // 车辆图层
            goodsBuildinglayer: null, // 车辆图层
            popuplayer: null, // 全局的弹框图层
            popupContent: null, // 弹框内容对象
            clickevevtslayer: null, //事件详情图层
            clickvideoslayer:null //视频树播放图层
        },
        eventWarningAlarmData: [], // 紧急事件列表
    },
    mutations: {
        // 设置菜单名称对应的编码
        setAllAuthority (state, item) {
            state.allAuthority = item
        },
        // 小区列表
        areaListTotal (state, payload) {
            console.log('areaListTotal', payload)
            state.areaList = payload
        },
        // 获取设备搜索列表
        searchEquipList (state, payload) {
            state.equipList = payload
        },
        changeMenuId (state, payload) {
            state.menuId = payload
            window.sessionStorage.setItem('menuId', payload)
        },
        changeRoute (state, payload) {
            state.route = payload
            window.sessionStorage.setItem('router', payload)
        },
        clearRoute (state) {
            state.route = []
        },
        changePagination (state, payload) {
            state.pagination[payload.name] = payload.value
        },
        clear (state, payload) {
            state[payload] = {}
        },
        parseAuthority (state, payload) {
            state.authority.appendable = payload & 1
            state.authority.editable = payload & 2
            state.authority.deletable = payload & 4
            state.authority.pubable = payload & 8
        },
        // 设置是否使用缓存
        setStorageEnable: function (state, enable) {
            state.storage.enable = enable
        },
        // 设置缓存，缓存的键名是storage.path，缓存键值是storage.data
        setStorageData: function (state, storage) {
            !state.storage.data[storage.path] && (state.storage.data[storage.path] = {})
            Object.assign(state.storage.data[storage.path], storage.data)
        },
        setMenu (state) {
            state.showMenu = !state.showMenu
        },
        setSendArr (state, payload) {
            state.sendArr = payload
        },
        setFloorArr (state, payload) {
            state.floorArr = payload
        },
        // setFloor (state, payload) {
        //     state.showFloor = !state.showFloor
        // },
        // 紧急通信
        // updateSession (state, payload) {
        //     state.sessionList.filter(item => {
        //         return item.deviceCode === payload.deviceCode
        //     }).status = payload.status
        // },
        setSessionDialog (state, payload) {
            state.sessionDialog = payload
        },
        setAddressList (state, payload) {
            state.addressList = payload
        },
        setSessionList (state, payload) {
            state.sessionList = payload
        },
        setIsInfo (state, payload) {
            state.isInfo = payload
        },
        delAddressList (state, payload) {
            state.addressList.splice(payload, 1)
        },
        delSessionList (state, payload) {
            console.log(state.sessionList, payload)
            state.sessionList.splice(payload, 1)
        },
        setInfoMessage (state, payload) {
            state.infoMessage = payload
        },
        setReadyMeeting (state, payload) {
            state.isReadyMeeting = payload
        },
        setMinCommunicationDialog (state, payload) {
            state.isMinCommunicationDialog = payload
        },
        setCircleArea (state, payload) {
            state.isCircleArea = payload
        },
        setForbidCircleArea (state, payload) {
            state.isForbidCircleArea = payload
        },
        // 安防人员车辆预警数据
        setSecurityWarningData (state, payload) {
            state.securityWarningData = payload
        },
        setmap (state, item) {
            state.map = item
        },
        setkeyplacesmarklayer (state, item) {
            // 重点机构图层
            state.mapmarks.keyplacesmarklayer = item
        },
        setkeysitemarklayer (state, item) {
            // 重点场所图层
            state.mapmarks.keysitemarklayer = item
        },
        setheatMap (state, item) {
            // 事件热力图图层
            state.mapmarks.heatMap = item
        },
        setvillagemarklayer (state, item) {
            // 小区图层
            state.mapmarks.villagemarklayer = item
        },
        setwanggelayer (state, item) {
            // 网格图层
            state.mapmarks.wanggelayer = item
        },
        setsimpleSoldierlayer (state, item) {
            // 单兵图层
            state.mapmarks.simpleSoldierlayer = item
        },
        setARPositionlayer (state, item) {
            // AR点位图层
            state.mapmarks.ARPositionlayer = item
        },
        setkeypersonlayer (state, item) {
            // 关注人员图层
            state.mapmarks.keypersonlayer = item
        },
        setkeypersonlinelayer (state, item) {
            // 关注人员轨迹图层
            state.mapmarks.keypersonlinelayer = item
        },
        setvideolayer (state, item) {
            // 视频图层
            state.mapmarks.videolayer = item
        },
        setrunwaylayer (state, item) {
            // 河道图层
            state.mapmarks.runwaylayer = item
        },
        setvehiclelayer (state, item) {
            // 车辆图层
            state.mapmarks.vehiclelayer = item
        },
        seteventslayer (state, item) {
            // 公共管理事件图层
            state.mapmarks.eventslayer = item
        },
        setgoodsBuildinglayer (state, item) {
            // 人房信息
            state.mapmarks.goodsBuildinglayer = item
        },
        setpopuplayer (state, item) {
            // 弹框图层
            state.mapmarks.popuplayer = item
        },
        setpopupContent (state, item) {
            // 弹框图层内容太
            state.mapmarks.popupContent = item
        },
        setclickevevtslayer (state, item) {
            // 四平台 事件点击详情
            state.mapmarks.clickevevtslayer = item
        },  
        setclickvideoslayer (state, item) {
            // 四平台 事件点击详情
            state.mapmarks.clickvideoslayer = item
        },
        removepopup (state) {
            if (state.mapmarks.popuplayer) {
                state.mapmarks.popuplayer.setPosition(undefined)
            }
        },
        seteventWarningAlarmData (state, item) {
            if (item) {
                state.eventWarningAlarmData = item
            }
        }
    },
    getters: {
    },
    actions: {
        parseAuthority ({commit, state}, payload) {
            let id = state.menuId || window.sessionStorage.getItem('menuId')
            return new Promise((resolve, reject) => {
                getAuthorityCode({menu_id: id}).then(res => {
                    // 当出现错误的请求，比如身份信息过期，返回403错误码的时候，res的值是undefined，所以直接返回，axios拦截器中拦截，并提示用户登陆超时
                    if (!res) {
                        return false
                    }
                    if (res.data.code == 200) {
                        commit('parseAuthority', res.data.data.code)
                    } else {
                        reject(res.data.data)
                    }
                }).catch(error => {
                    reject(error.message)
                })
            })
        },
        closeCommunicationDialog ({commit, state}, payload) {
            commit('setSessionDialog', false)
            commit('setAddressList', [])
            commit('setSessionList', [])
            commit('setIsInfo', true)
        }
    }
})
